import 'package:flutter_admin/pages/common/cry/cry.dart';
import 'package:flutter_admin/pages/common/cry/cry_button_bar.dart';
import 'package:flutter_admin/pages/common/cry/cry_buttons.dart';
import 'package:flutter_admin/pages/common/cry/cry_dialog.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select_date.dart';
import 'package:flutter_admin/pages/common/cry/model/order_item_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/cry_data_table.dart';
import 'package:flutter_admin/api/odoo/product_productionorderdetail_odoo_api.dart';
import 'package:flutter_admin/constants/enum.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/pages/common/cry/model/page_model.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/models/odoo/product_productionorderdetail.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/utils/adaptive_util.dart';

class ProductProductionorderdetailList extends StatefulWidget {

  late String? orderId;

  ProductProductionorderdetailList({required this.orderId,Key? key}) : super(key: key);

  @override
  _ProductProductionorderdetailListState createState() => _ProductProductionorderdetailListState();
}

class _ProductProductionorderdetailListState extends State<ProductProductionorderdetailList> {
  final GlobalKey<CryDataTableState> tableKey = GlobalKey<CryDataTableState>();
  final GlobalKey<ScaffoldState> scaffoldStateKey = GlobalKey<ScaffoldState>();
  PageModel page = PageModel(orders: [OrderItemModel(column: 'create_date',asc: false)]);
  ProductProductionorderdetail productProductionorderdetail = ProductProductionorderdetail();
  @override
  void initState() {
    productProductionorderdetail.orderId = widget.orderId;
    super.initState();
    WidgetsBinding.instance!.addPostFrameCallback((c) {
      _query();
    });
  }

  @override
  Widget build(BuildContext context) {
    CryDataTable table = CryDataTable(
      key: tableKey,
      title: "数据列表",
      onPageChanged: (firstRowIndex) {
        page.current = (firstRowIndex ~/ page.size + 1);
        _loadData();
      },
      onRowsPerPageChanged: (int size) {
        page.size = size;
        page.current = 1;
        _loadData();
      },
      onSelectChanged: (Map selected) {
        this.setState(() {});
      },
      columns: [
	        DataColumn(
          label: Text('物料编号'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
	        DataColumn(
          label: Text('物料名称'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
	        DataColumn(
          label: Text('规格型号'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
	        DataColumn(
          label: Text('表面处理'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
	        DataColumn(
          label: Text('明细类型'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
        DataColumn(
          label: Text('车间名称'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
	        DataColumn(
          label: Text('单件数量'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
	        DataColumn(
          label: Text('单台数量'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
	        DataColumn(
          label: Text('计划数量'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
	        DataColumn(
          label: Text('计量单位'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
      ],
      getCells: (m) {
        ProductProductionorderdetail productProductionorderdetail = ProductProductionorderdetail.fromMap(m);
        return [
          DataCell(Text(productProductionorderdetail.productspec ?? '--')),
          DataCell(Text(productProductionorderdetail.productname ?? '--')),
          DataCell(Text(productProductionorderdetail.productstyle ?? '--')),
          DataCell(Text(productProductionorderdetail.plate ?? '--')),
          DataCell(Text(productProductionorderdetail.plantype ?? '--')),
          DataCell(Text(productProductionorderdetail.workshopname ?? '--')),
          DataCell(Text(productProductionorderdetail.unitqty ?? '--')),
          DataCell(Text(productProductionorderdetail.totalunitqty ?? '--')),
          DataCell(Text(productProductionorderdetail.planqty ?? '--')),
          DataCell(Text(productProductionorderdetail.unitname ?? '--')),
        ];
      },
    );
    var result = Scaffold(
      key: scaffoldStateKey,
      appBar: AppBar(
        leading: Builder(
          builder: (BuildContext context) {
            final ModalRoute<Object?>? parentRoute = ModalRoute.of(context);
            final bool canPop = parentRoute?.canPop ?? false;
            if (canPop) {
              return BackButton();
            } else {
              return SizedBox.shrink();
            }
          },
        ),
        title: Text('BOM明细数据列表'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          SizedBox(height: 10),
          Expanded(child: SingleChildScrollView(child: table)),
        ],
      ),
    );
    return result;
  }

  _query() {
    tableKey.currentState?.pageTo(0);
    _loadData();
  }

  _loadData() async {
    ResponseBodyApi responseBodyApi = await ProductProductionorderdetailOdooApi.page(
        data:{"page": page,"domainList":productProductionorderdetail.toDomainList()});
    if(responseBodyApi.success!){
      page = responseBodyApi.data != null ? responseBodyApi.data : PageModel();
      tableKey.currentState!.loadData(page);
    }
  }

  _sort(column, {ascending}) {
    page.orders[0].column = column;
    page.orders[0].asc = ascending ?? !page.orders[0].asc!;
    _query();
  }

}